package org.carpediem.conman.rest;

import org.carpediem.conman.entity.Firm;
import org.carpediem.schema.conman.FirmInfo;

import javax.servlet.ServletContext;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author Mark Petrovic
 */
@Path("/firms")
public class FirmsResource extends AbstractResource {
    private static Logger logger = Logger.getLogger(FirmsResource.class.getName());

    public FirmsResource(@Context UriInfo uriInfo, @Context ServletContext servletContext) {
        super(uriInfo, servletContext);
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public FirmBag getFirms() {
        List<Firm> firms = firmDao.getAllFirms();
        return entityToBag(firms, FirmBag.class);
    }

    @POST
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    public Firm newFirm(JAXBElement<FirmInfo> request) throws IOException {
        FirmInfo fInfo = request.getValue();
        logger.info(toXml(fInfo));

        requireNewFirm(fInfo);

        return firmDao.add(fInfo);
    }
}
